package com.custom.base.drawable

import android.R
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.LayerDrawable
import android.graphics.drawable.StateListDrawable

/**
 * 作者：
 * 时间：2021/1/14
 * 描述：SDDrawable
 */
class SDDrawable @JvmOverloads constructor(
    layers: List<Drawable> = listOf(
        GradientDrawable(),
        GradientDrawable()
    )
) : LayerDrawable(layers.toTypedArray()) {
    private var mDrawableFirst: GradientDrawable? = null
    private var mDrawableSecond: GradientDrawable? = null
    private var mStrokeWidthLeft = 0
    private var mStrokeWidthTop = 0
    private var mStrokeWidthRight = 0
    private var mStrokeWidthBottom = 0
    private var mCornerTopLeft = 0f
    private var mCornerTopRight = 0f
    private var mCornerBottomLeft = 0f
    private var mCornerBottomRight = 0f
    private fun init() {
        mDrawableFirst = getDrawable(0) as GradientDrawable
        mDrawableSecond = getDrawable(1) as GradientDrawable
        mDrawableFirst?.apply {
            shape = GradientDrawable.RECTANGLE
            shape = GradientDrawable.RECTANGLE
        }
        color(DEFAULT_COLOR)
    }

    /**
     * 透明度
     *
     * @param alpha
     * @return
     */
    fun alpha(alpha: Int): SDDrawable {
        setAlpha(alpha)
        return this
    }

    /**
     * 图片颜色
     *
     * @param color
     * @return
     */
    fun color(color: Int): SDDrawable {
        mDrawableSecond?.setColor(color)
        return this
    }

    /**
     * 边框颜色
     *
     * @param color
     * @return
     */
    fun strokeColor(color: Int): SDDrawable {
        mDrawableFirst?.setColor(color)
        return this
    }

    /**
     * 设置圆角
     *
     * @param topLeft
     * @param topRight
     * @param bottomLeft
     * @param bottomRight
     * @return
     */
    fun corner(
        topLeft: Float,
        topRight: Float,
        bottomLeft: Float,
        bottomRight: Float
    ): SDDrawable {
        mCornerTopLeft = topLeft
        mCornerTopRight = topRight
        mCornerBottomLeft = bottomLeft
        mCornerBottomRight = bottomRight
        mDrawableFirst?.apply {
            cornerRadii = floatArrayOf(
                mCornerTopLeft, mCornerTopLeft, mCornerTopRight, mCornerTopRight, mCornerBottomRight,
                mCornerBottomRight, mCornerBottomLeft, mCornerBottomLeft
            )
        }
        mDrawableSecond?.apply {
            cornerRadii = floatArrayOf(
                mCornerTopLeft, mCornerTopLeft, mCornerTopRight, mCornerTopRight, mCornerBottomRight,
                mCornerBottomRight, mCornerBottomLeft, mCornerBottomLeft
            )
        }
        return this
    }

    fun cornerAll(radius: Float): SDDrawable {
        corner(radius, radius, radius, radius)
        return this
    }

    fun cornerTopLeft(radius: Float): SDDrawable {
        corner(radius, mCornerTopRight, mCornerBottomLeft, mCornerBottomRight)
        return this
    }

    fun cornerTopRight(radius: Float): SDDrawable {
        corner(mCornerTopLeft, radius, mCornerBottomLeft, mCornerBottomRight)
        return this
    }

    fun cornerBottomLeft(radius: Float): SDDrawable {
        corner(mCornerTopLeft, mCornerTopRight, radius, mCornerBottomRight)
        return this
    }

    fun cornerBottomRight(radius: Float): SDDrawable {
        corner(mCornerTopLeft, mCornerTopRight, mCornerBottomLeft, radius)
        return this
    }

    /**
     * 边框宽度
     *
     * @param left
     * @param top
     * @param right
     * @param bottom
     * @return
     */
    fun strokeWidth(left: Int, top: Int, right: Int, bottom: Int): SDDrawable {
        mStrokeWidthLeft = left
        mStrokeWidthTop = top
        mStrokeWidthRight = right
        mStrokeWidthBottom = bottom
        setLayerInset(1, mStrokeWidthLeft, mStrokeWidthTop, mStrokeWidthRight, mStrokeWidthBottom)
        return this
    }

    fun strokeWidthAll(width: Int): SDDrawable {
        strokeWidth(width, width, width, width)
        return this
    }

    fun strokeWidthLeft(width: Int): SDDrawable {
        strokeWidth(width, mStrokeWidthTop, mStrokeWidthRight, mStrokeWidthBottom)
        return this
    }

    fun strokeWidthTop(width: Int): SDDrawable {
        strokeWidth(mStrokeWidthLeft, width, mStrokeWidthRight, mStrokeWidthBottom)
        return this
    }

    fun strokeWidthRight(width: Int): SDDrawable {
        strokeWidth(mStrokeWidthLeft, mStrokeWidthTop, width, mStrokeWidthBottom)
        return this
    }

    fun strokeWidthBottom(width: Int): SDDrawable {
        strokeWidth(mStrokeWidthLeft, mStrokeWidthTop, mStrokeWidthRight, width)
        return this
    }

    companion object {
        private val DEFAULT_COLOR = Color.parseColor("#ffffff")
        // -------------------------------StateListDrawable
        /**
         * 获得可以根据状态变化的drawable背景
         *
         * @param none     正常状态时候背景
         * @param focus    获得焦点时候的背景
         * @param selected 选中时候的背景
         * @param pressed  按下时候的背景
         * @return
         */
        @JvmStatic
        fun getStateListDrawable(
            none: Drawable?,
            focus: Drawable?,
            selected: Drawable?,
            pressed: Drawable?
        ): StateListDrawable {
            val drawable = StateListDrawable()
            if (none != null) {
                drawable.addState(stateNone, none)
            }
            if (focus != null) {
                drawable.addState(stateFocus, focus)
            }
            if (selected != null) {
                drawable.addState(stateSelected, selected)
            }
            if (pressed != null) {
                drawable.addState(statePressed, pressed)
            }
            return drawable
        }

        // -------------------------------States
        val stateNone: IntArray
            get() = intArrayOf(
                -R.attr.state_focused,
                -R.attr.state_selected,
                -R.attr.state_pressed
            )

        val stateFocus: IntArray
            get() = intArrayOf(
                R.attr.state_focused,
                -R.attr.state_selected,
                -R.attr.state_pressed
            )

        val stateSelected: IntArray
            get() = intArrayOf(
                -R.attr.state_focused,
                R.attr.state_selected,
                -R.attr.state_pressed
            )

        val statePressed: IntArray
            get() = intArrayOf(
                -R.attr.state_focused,
                -R.attr.state_selected,
                R.attr.state_pressed
            )
    }

    init {
        init()
    }
}